// pages/qrcode/qrcode.html.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    confirmBtn: { content: '确定', variant: 'base' },
    disabled:false,
    isShowConfirm:false,
    isTeacher:false,
    good:{},
    kind:{},
    bindInfo:[],
    barcode:"",
    username:"",
    isBind:false,
    devices:[]
  },

  onLoad(options) {
    wx.request({
      url: app.globalData.url+'/wechat/student_devices',
      data: {
        username: app.globalData.localuserinfo.username,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: res2 => {
        if(res2.statusCode==200){
          this.setData({devices:res2.data})
        }
      },
      fail:()=>{
        console.log('fail')
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  scanCode() {
    const that=this
    wx.scanCode({
      success(res) {
        const barcode=res.result;
        that.setData({barcode});
        that.getDeviceInfo();
      },
      fail(e){
        wx.showToast({
          title: '识别失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  getDeviceInfo(){
    const username=app.globalData.localuserinfo.username;
    const isTeacher=app.globalData.localuserinfo.isTeacher;
    this.setData({username,isTeacher});
    wx.request({     
      url: `${app.globalData.url}/wechat/deviceInfo?c=${this.data.barcode}`,
      method: 'get',
      header: {
        'content-type': 'application/json'
      },
      success: res => {
        const data=res.data;
        if (res.data.status == 'fail') {
          wx.showToast({
            title: '获取物品信息失败'+res.data.err,
            icon: 'none',
            duration: 2000
          })
        }else{
          const {good,kind,bindInfo}=data;
          const myBind=bindInfo.find(d=>d.username===this.data.username);
          this.setData({good,kind,bindInfo,isBind:myBind?true:false})
        }
      }
    })
  },


  showConfirm:function(){
    this.setData({isShowConfirm:true})
  },
  closeDialog:function(){
    this.setData({isShowConfirm:false})
  },

  repairs:function(){
    wx.navigateTo({
      url: '../device_repairs/device_repairs?barcode='+this.data.good.barcode,
      fail:res=>{
        console.log("fail");
        console.log(res);
      },
    })
  },

  bind: function () {
    wx.request({
      url: `${app.globalData.url}/wechat/bind_device`,
      method: 'post',
      data: {
        username: this.data.username,
        barcode: this.data.barcode
      },
      header: {
        'content-type': 'application/json'
      },
      success: res => {
        this.setData({isShowConfirm:false});
        this.getDeviceInfo()
      }
    })
  },

  view(){
    if(!this.data.kind.file){
     return  wx.showToast({
        title: '未上传资料',
        icon: 'none',
        duration: 2000
      })
    }
    wx.navigateTo({
      url: '../device_resource/device_resource?file='+this.data.kind.file,
      fail:res=>{
        console.log("fail");
        console.log(res);
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})